﻿////////////////////////////////////////////
/*
 *本代码由代码生成器自动生成，请不要更改此文件的任何代码。如需要进行修改，请在对应的扩展类中添加新类，并使用partial修饰符。
 *生成时间：2015-06-18
 *生成者：JerzyJiang
 */
////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Data;
using Model;
using DBUtility;
using CommonUtility;

namespace DAL
{
	public partial class Function
	{	
		#region 增删改
		/// <summary>
		/// 新增
		/// </summary>
		/// <returns></returns>
		public static bool Add(FunctionInfo _model)
		{
			if (_model.RID != 0) return Edit(_model, null);
			else return Add(_model, null);
		}
		
		/// <summary>
		/// 编辑
		/// </summary>
		/// <returns></returns>
		public static bool Edit(FunctionInfo _model)
		{
			if (_model.RID == 0) return Add(_model, null);
			else return Edit(_model, null);
		}
		
		/// <summary>
		/// 删除
		/// </summary>
		/// <returns></returns>
		public static bool Delete(FunctionInfo _model)
		{
			bool res = true;
			try
			{
				string strSQL = "DELETE [tFunction] WHERE [RID]=@RID";
				SqlParameter[] para = new SqlParameter[1];
				para[0] = new SqlParameter("@RID",SqlDbType.Int,4){Value=_model.RID};
				int _num = Common.ToInt32(SqlHelper.ExecuteNonQuery(strSQL,para));
				if (_num == 0)
					res = false;
			}
			catch (Exception ex)
			{
				res = false;
				Common.Application_Error_Server("Function.cs/Delete()", ex);
				throw ex;
			}
			return res;
		}
		
		#region 带事务
		/// <summary>
		/// 新增
		/// </summary>
		/// <returns></returns>
		public static bool Add(FunctionInfo _model,SqlTransaction _myTran)
		{
			if (_model.RID != 0) return Edit(_model,_myTran);
			bool res = true;
			try
			{
				string strSQL = @"
INSERT INTO [tFunction](
	[funID],
	[funDesc],
	[flag],
	[lpt],
	[whom]
)VALUES(
	@funID,
	@funDesc,
	@flag,
	getDate(),
	@whom
);
SELECT @@IDENTITY AS ID;";
				SqlParameter[] para = new SqlParameter[4];
				para[0] = new SqlParameter("@funID",SqlDbType.VarChar,4){Value=_model.FunID};				
				para[1] = new SqlParameter("@funDesc",SqlDbType.NVarChar,50){Value=_model.FunDesc};				
				para[2] = new SqlParameter("@flag",SqlDbType.Char,1){Value=_model.Flag};				
				para[3] = new SqlParameter("@whom",SqlDbType.NVarChar,30){Value=_model.Whom};				
				
				int _id = Common.ToInt32(_myTran == null ? SqlHelper.ExecuteScalar(strSQL, para) : SqlHelper.ExecuteScalar(strSQL, para, _myTran));
				if (_id == 0)
					res = false;
				else
					_model.RID = _id;
			}
			catch (Exception ex)
			{
				res = false;
				Common.Application_Error_Server("Function.cs/Add()", ex);
				throw ex;
			}
			return res;
		}
		
		/// <summary>
		/// 编辑
		/// </summary>
		/// <returns></returns>
		public static bool Edit(FunctionInfo _model,SqlTransaction _myTran)
		{
			if (_model.RID == 0) return Add(_model,_myTran);
			bool res = true;
			try
			{
				string strSQL = @"
UPDATE [tFunction] SET
	[funID]= @funID,
	[funDesc]= @funDesc,
	[flag]= @flag,
	[lpt]= getDate(),
	[whom]= @whom
WHERE 
	[RID]= @RID";
				SqlParameter[] para = new SqlParameter[5];
				para[0] = new SqlParameter("@funID",SqlDbType.VarChar,4){Value=_model.FunID};		
				para[1] = new SqlParameter("@funDesc",SqlDbType.NVarChar,50){Value=_model.FunDesc};		
				para[2] = new SqlParameter("@flag",SqlDbType.Char,1){Value=_model.Flag};		
				para[3] = new SqlParameter("@whom",SqlDbType.NVarChar,30){Value=_model.Whom};		
				para[4] = new SqlParameter("@RID",SqlDbType.Int,4){Value=_model.RID};
	
				int _num = Common.ToInt32(_myTran == null ? SqlHelper.ExecuteNonQuery(strSQL, para) : SqlHelper.ExecuteNonQuery(strSQL, para, _myTran));
				if (_num == 0)
					res = false;
			}
			catch (Exception ex)
			{
				res = false;
				Common.Application_Error_Server("Function.cs/Edit()", ex);
				throw ex;
			}
			return res;
		}
		
		/// <summary>
		/// 删除
		/// </summary>
		/// <returns></returns>
		public static bool Delete(FunctionInfo _model,SqlTransaction _myTran)
		{
			bool res = true;
			try
			{
				string strSQL = "DELETE [tFunction] WHERE [RID]=@RID";
				SqlParameter[] para = new SqlParameter[1];
				para[0] = new SqlParameter("@RID",SqlDbType.Int,4){Value=_model.RID};
				int _num = Common.ToInt32(SqlHelper.ExecuteNonQuery(strSQL,para, _myTran));
				if (_num == 0)
					res = false;
			}
			catch (Exception ex)
			{
				res = false;
				Common.Application_Error_Server("Function.cs/Delete()", ex);
				throw ex;
			}
			return res;
		}
		#endregion
		#endregion

		/// <summary>
		/// 获取符合条件记录的条数
		/// </summary>
		/// <param name="_condition">条件语句，不包含“where”</param>
		/// <returns></returns>
		public static IList<FunctionInfo> GetList(string _condition)
		{    
			IList<FunctionInfo> list = new List<FunctionInfo>();
			try
			{
				string strSQL = @"
SELECT 
	[RID],
	[funID],
	[funDesc],
	[flag],
	[lpt],
	[whom]
FROM [tFunction]";
				if (!string.IsNullOrEmpty(_condition))
					strSQL += " WHERE " + _condition;
				
                DataSet mySet = new DataSet();
                SqlHelper.FillDataset(strSQL, mySet, "data", true);
                if (mySet != null && mySet.Tables.Count > 0)
                {
                    foreach (DataRow dr in mySet.Tables["data"].Rows)
                    {
						FunctionInfo model = new FunctionInfo();
						model.RID = Common.ToInt32(dr["RID"]);
						model.FunID = Common.ToString(dr["funID"]);
						model.FunDesc = Common.ToString(dr["funDesc"]);
						model.Flag = Common.ToString(dr["flag"]);
						model.Lpt = Common.ToString(dr["lpt"]);
						model.Whom = Common.ToString(dr["whom"]);
						list.Add(model);
					}
				}
				mySet.Dispose();
			}
			catch (Exception ex)
			{
				Common.Application_Error_Server("Function.cs/GetList("+_condition+")", ex);
			}
			return list;	
		}
		
		/// <summary>
		/// 获取符合条件记录的条数
		/// </summary>
		/// <param name="_condition">条件语句，不包含“where”</param>
		/// <param name="_parms">条件中的参数</param>
		/// <returns></returns>
		public static IList<FunctionInfo> GetList(string _condition, SqlParameter[] _parms)
		{    
			IList<FunctionInfo> list = new List<FunctionInfo>();
			try
			{
				string strSQL = @"
SELECT 
	[RID],
	[funID],
	[funDesc],
	[flag],
	[lpt],
	[whom]
FROM [tFunction]";
				if (!string.IsNullOrEmpty(_condition))
					strSQL += " WHERE " + _condition;
				
                DataSet mySet = new DataSet();
                SqlHelper.FillDataset(strSQL, mySet, "data", _parms, true);
                if (mySet != null && mySet.Tables.Count > 0)
                {
                    foreach (DataRow dr in mySet.Tables["data"].Rows)
                    {
						FunctionInfo model = new FunctionInfo();
						model.RID = Common.ToInt32(dr["RID"]);
						model.FunID = Common.ToString(dr["funID"]);
						model.FunDesc = Common.ToString(dr["funDesc"]);
						model.Flag = Common.ToString(dr["flag"]);
						model.Lpt = Common.ToString(dr["lpt"]);
						model.Whom = Common.ToString(dr["whom"]);
						list.Add(model);
					}
				}
				mySet.Dispose();
			}
			catch (Exception ex)
			{
				Common.Application_Error_Server("Function.cs/GetList("+_condition+")", ex);
			}
			return list;	
		}		
		
		/// <summary>
        /// 根据条件语句取得符合条件的数据表
        /// </summary>
        /// <param name="_condition">条件语句，不包含“where”</param>
        /// <returns></returns>
        public static DataTable GetTable(string _condition)
        {
            DataSet mySet = new DataSet();
			try
			{
				string strSQL = @"
SELECT 
	[RID],
	[funID],
	[funDesc],
	[flag],
	[lpt],
	[whom]
FROM [tFunction]";
				if (!string.IsNullOrEmpty(_condition))
					strSQL += " WHERE " + _condition;
				
                SqlHelper.FillDataset(strSQL, mySet, "data", true);
			}
			catch (Exception ex)
			{
				Common.Application_Error_Server("Function.cs/GetTable("+_condition+")", ex);
			}
			return mySet!=null && mySet.Tables.Count>0 ? mySet.Tables[0] : null;	
        }
        
        /// <summary>
        /// 获取符合条件记录的条数。
		/// 在使用SQL分页是可以用到，与getTable配合使用
        /// </summary>
        /// <param name="_condition">条件语句，不包含“where”</param>
        /// <returns></returns>
        public static int Count(string _condition)
        {
			int result = 0;
			try
			{
				string strSQL = "SELECT COUNT(*) FROM [tFunction]";
				if (!string.IsNullOrEmpty(_condition))
					strSQL += " WHERE " + _condition;
				
                result = Common.ToInt32(SqlHelper.ExecuteScalar(strSQL));
			}
			catch (Exception ex)
			{
				Common.Application_Error_Server("Function.cs/Count("+_condition+")", ex);
			}
			return result;	
        }
		
        /// <summary>
        /// 获取分页数据（数据库分页）
        /// </summary>
        /// <param name="_pageIndex">页码，从1开始</param>
        /// <param name="_pageSize">分页大小（每页显示记录数）</param>
        /// <param name="_condition">条件语句，不包含“where”（在组装条件前需要对条件值进行单引号处理）</param>
        /// <param name="_orderStr">排序条件，如：ID DESC</param>
        /// <param name="_total">返回符合条件的总记录数</param>
        /// <param name="_message">存储过程的执行结果</param>
        /// <returns>当前页的数据表，没有数据返回Null</returns>
		public static DataTable GetPageData(int _pageIndex, int _pageSize, string _condition, string _orderStr, ref int _total, ref string _message)
		{
			int recordCount = 0; //本次查到返回的记录数目
			int pageCount = 0; //返回本次查询能够返回的页数
			if (string.IsNullOrEmpty(_orderStr))
				_orderStr = "RID DESC";

			return SqlHelper.GetPageData("tFunction",0,1,_condition,_orderStr,"*",_pageIndex,_pageSize,ref _total,ref recordCount, ref pageCount, ref _message);
		}
		
		/// <summary>
        /// 获取分页数据对象（数据库分页）
        /// </summary>
        /// <param name="_pageIndex">页码，从1开始</param>
        /// <param name="_pageSize">分页大小（每页显示记录数）</param>
        /// <param name="_condition">条件语句，不包含“where”（在组装条件前需要对条件值进行单引号处理）</param>
        /// <param name="_orderStr">排序条件，如：ID DESC</param>
        /// <param name="_total">返回符合条件的总记录数</param>
        /// <param name="_message">存储过程的执行结果</param>
        /// <returns>当前页的数据对象集合</returns>
		public static IList<FunctionInfo> GetPageObject(int _pageIndex, int _pageSize, string _condition, string _orderStr, ref int _total, ref string _message)
		{
			IList<FunctionInfo> list = new List<FunctionInfo>();
			try
			{
				if (string.IsNullOrEmpty(_orderStr))
					_orderStr = "RID DESC";
				int recordCount = 0; //本次查到返回的记录数目
				int pageCount = 0; //返回本次查询能够返回的页数
				DataTable td = SqlHelper.GetPageData("tFunction",0,1,_condition,_orderStr,"*",_pageIndex,_pageSize,ref _total,ref recordCount, ref pageCount, ref _message);
				if (td != null)
				{
					foreach (DataRow dr in td.Rows)
					{
						FunctionInfo model = new FunctionInfo();
						model.RID = Common.ToInt32(dr["RID"]);
						model.FunID = Common.ToString(dr["funID"]);
						model.FunDesc = Common.ToString(dr["funDesc"]);
						model.Flag = Common.ToString(dr["flag"]);
						model.Lpt = Common.ToString(dr["lpt"]);
						model.Whom = Common.ToString(dr["whom"]);
						list.Add(model);
					}
				}
			}
			catch (Exception ex)
			{
				Common.Application_Error_Server("Function.cs/GetPageData(2)", ex);
			}

			return list;
		}
		
		/// <summary>
        /// 根据ID取得实体
        /// </summary>
        /// <param name="_id">_id,即编号主键</param>
        /// <returns></returns>
        public static FunctionInfo GetModelByID(int _id)
        {
			FunctionInfo model = null;
			
			try
			{
				string strSQL = @"
SELECT
	[RID],
	[funID],
	[funDesc],
	[flag],
	[lpt],
	[whom]
FROM [tFunction]
WHERE 
	[RID]= @RID";
				SqlParameter[] para = new SqlParameter[1];
				para[0] = new SqlParameter("@RID",SqlDbType.Int,4){Value=_id};
				using (SqlDataReader sdr = SqlHelper.ExecuteReader(strSQL,para))
				{
					if (sdr.Read())
					{
						model = new FunctionInfo();
						model.RID = Common.ToInt32(sdr["RID"]);
						model.FunID = Common.ToString(sdr["funID"]);
						model.FunDesc = Common.ToString(sdr["funDesc"]);
						model.Flag = Common.ToString(sdr["flag"]);
						model.Lpt = Common.ToString(sdr["lpt"]);
						model.Whom = Common.ToString(sdr["whom"]);
					}
					sdr.Close();
				}
			}
			catch (Exception ex)
			{
				Common.Application_Error_Server("Function.cs/GetModelByID("+_id+")", ex);
			}
			return model;
        }
	}
}
